const Router = require('koa-router');
const  db  = require('./myDB/mydb');
var mongoose = require("mongoose");
const router = new Router();
const Tool = require("./tool");
router.get('/abc', (ctx, next) => {
    ctx.body = "123"
});

router.get('/', (ctx, next) => {
    // ctx.router available
    ctx.body = "主要路径"
});


// 添加用户
router.post('/add_user',async (ctx, next) => {
    const data = ctx.request.body
    var user_name = data.data.my_name
    var user_id = data.data.my_id
    var user_pwd = data.data.my_pwd
    var user_img = data.img
    console.log(user_img)
    console.log(user_id)
    console.log(data);
    ctx.body = await new Promise((reslove , reject)=> {
        db.db.User_model.User_model.find({id:user_id}).then((res) => {
            console.log(res) 
            if(res == ""){
                console.log("可以注册")
                reslove("可以注册")
                db.db.User_model.User_model.create({ name:user_name , id:user_id , pwd:user_pwd , img:user_img})
            }else {
                console.log("不可以注册")
                reslove("该账号已存在")
            }
        })
        
    })
});

// 登录 
router.post('/user_login',async (ctx, next) => {
    const data = ctx.request.body
    var user_id = data.data.user_id
    var user_pwd = data.data.user_pwd
    ctx.body=await new Promise((reslove,reject)=>{
        db.db.User_model.User_model.find({id:user_id,pwd:user_pwd}, function (err, docs) {
            if(docs == ''){
                console.log("空")
                reslove("找不到")
            }else{
                console.log(docs)
                reslove(docs , "登录成功")
                // reslove("登录成功")
            }
      });
    })
});

// 修改信息
router.post('/add_img',async (ctx, next) => {
    const data = ctx.request.body
    console.log(data)
    var find_id = data.find_id
    var name = data.name
    var img = data.img
    var query = {id: find_id}
    var updata = {$set: { name : name , img : img}}
    ctx.body = await new Promise((reslove , reject) => {
        db.db.User_model.User_model.updateOne(query , updata ,  (err , docs) => {
            if(docs){
                console.log(docs)
                reslove("修改成功" , docs)
            }else{
                console.log("失败")
            }
        })
    })
});

// 验证信息
router.post("/find_user" ,async (ctx , next) => {
    const data = ctx.request.body
    var id = data.user_id
    var name = data.user_name
    var find_user = {id , name}
    var a = Tool.RndNum(5)
    // console.log(a)
    ctx.body = await new Promise ((reslove , reject) => {
        db.db.User_model.User_model.findOne(find_user , (err , docs) => {
            if(docs){
                var b = ("找到了")
                let gun = {
                    text: b,
                    num: a
                }
                reslove(JSON.stringify(gun))
            }else if (docs == null){
                reslove("没找到")
                console.log("没有")
            }
        })
    })
});

// 修改信息
router.post('/change_pwd',async (ctx, next) => {
    const data = ctx.request.body
    console.log(data)
    var find = data.id
    var later_pwd = data.pwd
    var updata = { $set: {pwd : later_pwd} } 
    var find_id = {id : find}
    console.log(find_id)
    console.log(later_pwd)
   ctx.body = await new Promise((reslove , reject) =>{
    db.db.User_model.User_model.updateOne(find_id , updata ,  (err , docs) => {
        if(docs){
            console.log(docs)
            reslove("修改成功")
        }else{
            reslove("修改失败")
        }
    })
   })
});







exports.router=router
